<?php

namespace app\data\controller\base;

use think\Db;

/**
 * 基础 - 公共
 * Class
 * @package app\data\controller\base
 */
class Publics
{
	/**
	 * 实例对象
	 * @var object
	 */
	protected static $instance;

	/**
	 * 表默认配置
	 * @var \app\data\constants\TabConstants::TAb_CONFIG
	 */
	protected $tabConfig = null;

	/**
	 * 构造
	 */
	public function __construct()
	{
		$this->tabConfig = \app\data\constants\TabConstants::TAb_CONFIG;
	}

	/**
	 * 实例化
	 */
	public static function instance()
	{
		if (is_null(self::$instance)) {
			self::$instance = new static();
		}
		return self::$instance;
	}

	/**
	 * 表配置
	 */
	public function tab()
	{
		return (object)$this->tabConfig;
	}

	/**
	 * sn 绑定信息
	 * @return array|boolean
	 */
	public function snBinds($sn)
	{
		if (empty($sn)) {
			return false;
		}
		$finds = Db::name($this->tab()->ters)
			->where("sn = '{$sn}'")
			->field("id,user_id uid,sn,product_id proid,active_time aceTime,binding_time bindTime")
			->find();
		return $finds;
	}

	/**
	 * 查看-绑定信息存储
	 */
	public function checkBindStore($sn)
	{
		if (!$sn) {
			return false;
		}
		$check = Db::name($this->tab()->acte_stands)
			->where("sn = '{$sn}'")
			->count();
		if (!$check) {
			return true;
		}
		return false;
	}

	/**
	 * 获取-计算激活达标终端信息
	 */
	public function get_active_stands_sn($sn)
	{
		$check = $this->checkBindStore($sn);
		if ($check === false) {
			return Db::name($this->tab()->acte_stands)->where("sn = '{$sn}'")->find();
		}
		return false;
	}

	/**
	 * 获取数据表名
	 */
	public function getDataTable($proid)
	{
		if (!$proid) {
			return false;
		}
		$table = "";
		$ret = Db::name($this->tab()->pro)->where(["id" => $proid])->value("table");
		if ($ret) {
			$table = $ret;
		}
		return $table;
	}

	/**
	 * 查看-流失客户信息存储
	 */
	public function checkLostStore($uid)
	{
		if (!$uid) {
			return false;
		}
		$check = Db::name($this->tab()->lost)
			->where("uid = '{$uid}'")
			->count();
		if (!$check) {
			return true;
		}
		return false;
	}

	/**
	 * 获取-计算激活达标终端信息
	 */
	public function get_lost_stands_sn($uid)
	{
		$check = $this->checkBindStore($uid);
		if ($check === false) {
			return Db::name($this->tab()->lost)->where("uid = '{$uid}'")->find();
		}
		return false;
	}
}
